// miniprogram/pages/lgoin/login.js
var api = require('../../config/api.js');
const util = require('../../utils/util.js');
var app = getApp();
Page({
  data: {
    username: '',
    password: '',
    shopname:'',
    address:'',
    code: '',
    loginErrorCount: 0
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    // 页面渲染完成

    // request('auth/loginEr',this.data,'post').then(
    //   res => {
    //       console.log(res)
    //   }
    // )

  },
  onReady: function () {

  },
  onShow: function () {
    // 页面显示
  },
  onHide: function () {
    // 页面隐藏

  },
  onUnload: function () {
    // 页面关闭

  },

   
  accountLogin: function () {
    var that = this;
    if (this.data.password.length < 1 || this.data.username.length < 1) {
      wx.showModal({
        title: '错误信息',
        content: '请输入用户名和密码',
        showCancel: false
      });
      return false;
    }

    wx.request({
      url: api.AuthLoginByAccount,
      
      data: {
        username: that.data.username,
        password: that.data.password,
        // shopname: that.data.shopname,
        // address: that.data.address
      },
      method: 'POST',
      header: {
        'content-type': 'application/json'
    
      },
      success: function (res) {
        if (res.data.errno == 0) {
          that.setData({
            loginErrorCount: 0
          });
          app.globalData.hasLogin = true;
          console.log(res.data);
          wx.setStorage({
            key: "username",
            data: res.data.data.username,
          })
          wx.setStorage({
            key: "token",
            data: res.data.data.token,
            success: function () {

              wx.redirectTo({
                url: '../index/index'
              });
            }
          });
        } else {
          that.setData({
            loginErrorCount: that.data.loginErrorCount + 1
          });
          app.globalData.hasLogin = false;
          util.showErrorToast('账户登录失败');
        }
      }
    });
  },
  bindUsernameInput: function (e) {

    this.setData({
      username: e.detail.value
    });

  },
  bindPasswordInput: function (e) {

    this.setData({
      password: e.detail.value
    });

  },
  bindshopnameInput: function (e) {

    this.setData({
      shopname: e.detail.value
    });

  },
  bindaddressInput: function (e) {

    this.setData({
      address: e.detail.value
    });
    
  },
  bindCodeInput: function (e) {

    this.setData({
      code: e.detail.value
    });
  },
  // clearInput: function (e) {
  //   switch (e.currentTarget.id) {
  //     case 'clear-username':
  //       this.setData({
  //         username: ''
  //       });
  //       break;
  //     case 'clear-password':
  //       this.setData({
  //         password: ''
  //       });
  //       break;
  //     case 'clear-code':
  //       this.setData({
  //         code: ''
  //       });
  //       break;
  //   }
  // }
})